#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;
const int N = 1e5 + 10, M = 2e6 + 10;

int n, m;
int h[N], rh[N], e[M], ne[M], idx;
int dmin[N], dmax[N];
int price[N];
bool st[N];
int q[N];

void add(int h[], int a, int b){
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

void spfa(int h[], int dist[], int type){
    int hh = 0, tt = 1;
    if(type == 0){
        memset(dist, 0x3f, sizeof dmin);
        dist[1] = price[1];
        q[0] = 1;
    }else{
        memset(dist, -0x3f, sizeof dmin);
        dist[n] = price[n];
        q[0] = n;
    }

    while(hh != tt){
        int t = q[hh++];
        if(hh == N) hh = 0;

        st[t] = false;

        for (int u = h[t]; u != -1; u = ne[u]){
            int j = e[u];
            if((type == 0 && dist[j] > min(dist[t], price[j])) || (type == 1 && dist[j] < max(dist[t], price[j]))){
                if(type == 0){
                    dist[j] = min(dist[t], price[j]);
                }else{
                    dist[j] = max(dist[t], price[j]);
                }

                if(!st[j]){
                    q[tt++] = j;
                    
                    if(tt == N){
                        tt = 0;
                    }

                    st[j] = true;
                }
            }
        }
    }
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i ++){
        scanf("%d", &price[i]);
    }
    memset(h, -1, sizeof h);
    memset(rh, -1, sizeof rh);
    for (int i = 0; i < m; i ++){
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(h, a, b);
        add(rh, b, a);

        if(c == 2){
            add(h, b, a);
            add(rh, a, b);
        }
    }

    spfa(h, dmin, 0);
    spfa(rh, dmax, 1);

    int res = 0;
    for (int i = 1; i <= n; i++)
        res = max(res, dmax[i] - dmin[i]);

    printf("%d\n", res);

    return 0;
}